﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class AboutController : Controller
    {
        //
        // GET: /About/

        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "")
        {

            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var about = GswTblContent.SingleOrDefault("Where FunctionId=" + fid + " and CustomerId=" + GswTblUser.CurrentUser.CustomerId) ??
                        new GswTblContent { FunctionId = int.Parse(fid), CustomerId = GswTblUser.CurrentUser.CustomerId, CreateDate = DateTime.Today };
            ViewBag.id = id;
            return View(about);

        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(int id ,FormCollection c, string m = "", string f = "", string p = "", string fid = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var cm = id == 0 ? new GswTblContent() : GswTblContent.SingleOrDefault(id);
            cm.ContentTitle =f;
            cm.CreateDate = DateTime.Today;
            cm.Author = "";
            cm.ContentBrief = c["ContentBrief"];
            cm.ContentDesc = c["ContentDesc"]; ;
            cm.ContentPic = "";
            cm.ContentThumb = c["ContentThumb"];
            cm.FunctionId = int.Parse(fid);
            cm.CustomerId = GswTblUser.CurrentUser.CustomerId;
            cm.NewFlag = 0;
            cm.ContentNo = 0;


            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                        cm.Insert();
                        

                    }
                    else
                    {
                        cm.Update();
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";

            return View(cm);
        }

    }
}
